import 'package:desktop_drop/desktop_drop.dart';
import 'package:file_picker/file_picker.dart';

const String _allowFile = "silk";

class ConvertStatus {
  ConvertStatus._();

  static final String prepared = "已就绪";
  static final String converting = "正在转换";
  static final String completed = "转换完成";
  static final String failed = "转换失败";
}

class FileItem {
  final String name;
  final String type;
  final int size;
  String status;
  final String? path;
  String error = "";

  FileItem({
    required this.name,
    required this.type,
    required this.size,
    required this.status,
    this.path,
  });


  bool isError() {
    return status == ConvertStatus.failed;
  }
}

Future<List<FileItem>> pickFiles() async {
  FilePickerResult? inputResult = await FilePicker.platform.pickFiles(
    type: FileType.custom,
    allowedExtensions: [_allowFile],
    allowMultiple: true,
  );
  if (inputResult == null || inputResult.files.isEmpty) {
    return [];
  }
  return inputResult.files
      .map(
        (file) => FileItem(
          name: file.name,
          type: _allowFile,
          size: file.size,
          status: ConvertStatus.prepared,
          path: file.path,
        ),
      )
      .toList();
}

Future<List<FileItem>> drag2File(DropDoneDetails details) async {
  List<FileItem> files = [];
  for (var value in details.files) {
    if (!value.path.endsWith(_allowFile)) {
      continue;
    }
    final size = await value.length();
    files.add(
      FileItem(
        name: value.name,
        type: _allowFile,
        size: size,
        status: ConvertStatus.prepared,
        path: value.path,
      ),
    );
  }
  return files;
}
